package com.photoeditor.demo.ui.dialog;

import android.content.Context;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.photoeditor.R;
import com.photoeditor.demo.model.preference.SPDataManager;
import com.photoeditor.demo.statistics.StatisticsConstant;

import java.util.ArrayList;
import java.util.List;

public class VirtualRateBadDialog extends BaseVirtualRateDialog implements View.OnClickListener {

    private TextView mCancelView;
    private TextView mSureView;

    private List<CheckBox> mReasonBoxList;
    private List<String> mReasonStrList;

    private Context mContext;

    public static void showRateDialogVirtual(final Context context) {
        VirtualRateBadDialog sRateDialog = new VirtualRateBadDialog(context);
        sRateDialog.setCanceledOnTouchOutside(true);
        sRateDialog.show();
    }

    public VirtualRateBadDialog(Context context) {
        super(context);
    }

    @Override
    public void initDefaultView(Context context) {
        super.initDefaultView(context);
        setContentView(R.layout.dialog_virtual_rate_bad);

        mCancelView = findViewById(R.id.cancel);
        mCancelView.setOnClickListener(this);

        mSureView = findViewById(R.id.sure);
        mSureView.setOnClickListener(this);
        mSureView.setClickable(false);

        CheckBox mReason1 = findViewById(R.id.reason_1);
        CheckBox mReason2 = findViewById(R.id.reason_2);
        CheckBox mReason3 = findViewById(R.id.reason_3);
        CheckBox mReason4 = findViewById(R.id.reason_4);
        CheckBox mReason5 = findViewById(R.id.reason_5);
        CheckBox mReason6 = findViewById(R.id.reason_6);
        mReasonBoxList = new ArrayList<>();
        mReasonBoxList.add(mReason1);
        mReasonBoxList.add(mReason2);
        mReasonBoxList.add(mReason3);
        mReasonBoxList.add(mReason4);
        mReasonBoxList.add(mReason5);
        mReasonBoxList.add(mReason6);
        for (CheckBox checkBox : mReasonBoxList) {
            checkBox.setOnClickListener(this);
        }

        mReasonStrList = new ArrayList<>();
        mReasonStrList.add(StatisticsConstant.VALUE_REASON1);
        mReasonStrList.add(StatisticsConstant.VALUE_REASON2);
        mReasonStrList.add(StatisticsConstant.VALUE_REASON3);
        mReasonStrList.add(StatisticsConstant.VALUE_REASON4);
        mReasonStrList.add(StatisticsConstant.VALUE_REASON5);
        mReasonStrList.add(StatisticsConstant.VALUE_REASON6);

        DialogStatistic.rateBadShow(RateGuide.getWhere());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.reason_1:
            case R.id.reason_2:
            case R.id.reason_3:
            case R.id.reason_4:
            case R.id.reason_5:
            case R.id.reason_6:
                boolean hasCheck = false;
                for (CheckBox checkBox : mReasonBoxList) {
                    if (checkBox.isChecked()) {
                        hasCheck = true;
                        break;
                    }
                }
                if (hasCheck) {
                    mSureView.setClickable(true);
                    mSureView.setBackgroundResource(R.drawable.bg_apply_btn);
                } else {
                    mSureView.setClickable(false);
                    mSureView.setBackgroundResource(R.drawable.bg_apply_btn_no_clickable);
                }
                break;
            case R.id.cancel:
                dismiss();
                if (mListener != null && mListener.get() instanceof IRateDialogListener) {
                    ((IRateDialogListener) mListener.get()).onCloseClick();
                }
                DialogStatistic.rateBadClose(RateGuide.getWhere());
                break;
            case R.id.sure:
                dismiss();
                SPDataManager.setHasRated();

                List<String> reasons = new ArrayList<String>();
                for (int i = 0; i < mReasonBoxList.size(); i++) {
                    CheckBox reasonBox = mReasonBoxList.get(i);
                    if (reasonBox.isChecked()) {
                        String reason = mReasonStrList.get(i);
                        reasons.add(reason);
                        DialogStatistic.rateBadSub(RateGuide.getWhere(), reason);
                    }
                }
                if (mListener != null && mListener.get() instanceof IRateDialogListener) {
                    ((IRateDialogListener) mListener.get()).onReasonSubmit(reasons);
                }
                break;
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        DialogStatistic.rateBadClose(RateGuide.getWhere());
    }
}
